package com.example.uavstatus;

import static androidx.constraintlayout.widget.ConstraintLayoutStates.TAG;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button btnStartService, btnStopService, btnSettings;
    private TextView tvStatus;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        preferences = getSharedPreferences("uav_settings", MODE_PRIVATE);

        btnStartService = findViewById(R.id.btn_start_service);
        btnStopService = findViewById(R.id.btn_stop_service);
        btnSettings = findViewById(R.id.btn_settings);
        tvStatus = findViewById(R.id.tv_status);

        btnStartService.setOnClickListener(v -> startService());
        btnStopService.setOnClickListener(v -> stopService());
        btnSettings.setOnClickListener(v -> openSettings());

        updateStatus();

        // 验证 SDK 类是否可用
        try {
            Class.forName("dji.sdk.sdkmanager.DJISDKManager");
            Log.d(TAG, "DJI SDK classes are available");
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "DJI SDK classes not found: " + e.getMessage());
            Toast.makeText(this, "DJI SDK 未正确集成", Toast.LENGTH_LONG).show();
        }
    }

    private void startService() {
        Intent serviceIntent = new Intent(this, UAVStatusService.class);
        startService(serviceIntent);
        tvStatus.setText("服务已启动");
    }

    private void stopService() {
        Intent serviceIntent = new Intent(this, UAVStatusService.class);
        stopService(serviceIntent);
        tvStatus.setText("服务已停止");
    }

    private void openSettings() {
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivity(settingsIntent);
    }

    private void updateStatus() {
        String serverIp = preferences.getString("server_ip", "192.168.1.11");
        int controlPort = preferences.getInt("control_port", 4321);
        int dataPort = preferences.getInt("data_port", 1234);

        String status = String.format(
                "当前配置:\n目标IP: %s\n控制端口: %d\n数据端口: %d",
                serverIp, controlPort, dataPort
        );

        tvStatus.setText(status);
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateStatus();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    // 这是一个简单示例，实际应用中需要更复杂的手势识别
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // 检测是否在特定区域点击，例如屏幕右上角
        if (event.getX() > getResources().getDisplayMetrics().widthPixels * 0.8f &&
            event.getY() < getResources().getDisplayMetrics().heightPixels * 0.2f) {
            openSettings();
            return true;
        }
    }
    return super.onTouchEvent(event);
}
}
